package com.kll.carolinehmibiz.presentation.presenter

import android.content.Context
import com.kll.carolinehmibiz.OperationManager
import com.kll.carolinehmibiz.base.net.NetCode
import com.kll.carolinehmibiz.model.OperateListener
import com.kll.carolinehmibiz.presentation.BasePresenter
import com.kll.carolinehmibiz.presentation.listener.LoginProtocol
import io.reactivex.Observable
import io.reactivex.ObservableOnSubscribe
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers

/**
 * ====================================================
 * Created by Ziliang Cai on 2021/12/7 17:23
 * <a href="mailto:752777456@qq.com">Contact me</a>
 * ====================================================
 */
class LoginPresenter(context: Context, mView: LoginProtocol.IView) : BasePresenter(context), LoginProtocol.IPresenter {
    private var mView: LoginProtocol.IView? = mView

    internal var count = 0
    var sendCodeIsBusying = false
    var loginIsBusying = false

    override fun getVerifyCode(mobile: String) {
        if (count != 0 || sendCodeIsBusying) return
        sendCodeIsBusying = true
        mView?.showLoading()
        OperationManager.getInstancce().getVerifyCode(mobile, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                sendCodeIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    mView?.sendVerifyCodeSuccessed()
                }
                mView?.stopLoading()
                countDown()
            }

            override fun onFail(code: Int, message: String) {
                sendCodeIsBusying = false
                mView?.sendVerifyCodeFailed(code, message)
                mView?.stopLoading()
            }

        })
    }

    override fun login(phone: String, code: String) {
        if (loginIsBusying) return
        loginIsBusying = true
        mView?.showLoading()
        OperationManager.getInstancce().login(code, phone, object : OperateListener {
            override fun onSuccess(state: Int, obj: Any?) {
                loginIsBusying = false
                if (state == NetCode.NET_CODE_OK) {
                    mView?.loginSuccessed()
                }
                mView?.stopLoading()
            }

            override fun onFail(code: Int, message: String) {
                loginIsBusying = false
                mView?.loginFailed(code, message)
                mView?.stopLoading()
            }

        })
    }

    private fun countDown() {
        count = 60
        Observable.create(ObservableOnSubscribe<Int> { emitter ->
            while (count-- > 0) {
                Thread.sleep(1000)
                emitter.onNext(count)
            }
            emitter.onComplete()
        }).subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread()).subscribe(object :
            Observer<Int> {
            override fun onSubscribe(d: Disposable) {

            }

            override fun onNext(t: Int) {
                mView?.loadAreaCodeTime(t.toString(), true)
            }

            override fun onError(e: Throwable) {

            }

            override fun onComplete() {
                count = 0
                sendCodeIsBusying = false
                mView?.loadAreaCodeTime("获取验证码", false)
            }

        })
    }



}